import Vue from 'vue'
import PingYing from '@/common/pinyin.js'
import Private from './private.js'
import User from './user.js'
//排序
const Sort={"A":0,"B":1,"C":2,"D":3,"E":4,"F":5,"G":6,"H":7,"I":8,"J":9,"K":10,"L":11,"M":12,"N":13,"O":14,"P":15,"Q":16,"R":17,"S":18,"T":19,"U":20,"V":21,"W":22,"X":23,"Y":24,"Z":25,"_":26}
//返列表样式
const List =()=>{
	return[{"letter":"A","data":[]},{"letter":"B","data":[]},{"letter":"C","data":[]},{"letter":"D","data":[]},{"letter":"E","data":[]},{"letter":"F","data":[]},{"letter":"G","data":[]},{"letter":"H","data":[]},{"letter":"I","data":[]},{"letter":"J","data":[]},{"letter":"K","data":[]},{"letter":"L","data":[]},{"letter":"M","data":[]},{"letter":"N","data":[]},{"letter":"O","data":[]},{"letter":"P","data":[]},{"letter":"Q","data":[]},{"letter":"R","data":[]},{"letter":"S","data":[]},{"letter":"T","data":[]},{"letter":"U","data":[]},{"letter":"V","data":[]},{"letter":"W","data":[]},{"letter":"X","data":[]},{"letter":"Y","data":[]},{"letter":"Z","data":[]},{"letter":"_","data":[]}]
}
/* 下面的可以删了 */
function getName(){
	var familyNames = new Array(
		"$", "^", "@", "7", "1", "1", "哎", "爱", "阿", "安", 
		"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", 
		"褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
		"何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", 
		"陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章",
		"云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦", 
		"昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳",
		"酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", 
		"倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常",
		"乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", 
		"元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹",
	);
	var givenNames = new Array(
		"子璇", "淼", "国栋", "夫子", "瑞堂", "甜", "敏", "尚", "国贤", "贺祥", "晨涛", 
		"昊轩", "易轩", "益辰", "益帆", "益冉", "瑾春", "瑾昆", "春齐", "杨", "文昊", 
		"东东", "雄霖", "浩晨", "熙涵", "溶溶", "冰枫", "欣欣", "宜豪", "欣慧", "建政", 
		"美欣", "淑慧", "文轩", "文杰", "欣源", "忠林", "榕润", "欣汝", "慧嘉", "新建", 
		"建林", "亦菲", "林", "冰洁", "佳欣", "涵涵", "禹辰", "淳美", "泽惠", "伟洋", 
		"涵越", "润丽", "翔", "淑华", "晶莹", "凌晶", "苒溪", "雨涵", "嘉怡", "佳毅", 
		"子辰", "佳琪", "紫轩", "瑞辰", "昕蕊", "萌", "明远", "欣宜", "泽远", "欣怡", 
		"佳怡", "佳惠", "晨茜", "晨璐", "运昊", "汝鑫", "淑君", "晶滢", "润莎", "榕汕", 
		"佳钰", "佳玉", "晓庆", "一鸣", "语晨", "添池", "添昊", "雨泽", "雅晗", "雅涵", 
		"清妍", "诗悦", "嘉乐", "晨涵", "天赫", "玥傲", "佳昊", "天昊", "萌萌", "若萌"
	);
	var i = parseInt( 10* Math.random())*10 + parseInt(10 * Math.random());
	var familyName = familyNames[i];
	var j = parseInt( 10* Math.random())*10 + parseInt(10 * Math.random());
	var givenName = givenNames[i];
	var name = familyName + givenName;
	return name
}
// 生成测试数据
const UserInfoObject=()=>{
	let obj={}
	for(let i=1000;i<1100;i++){
		let nickname=getName()+'凑数无法聊天'
		obj[i]={
			id:i,
			nickname,
			figureurl:'https://cool-comm.oss-cn-shenzhen.aliyuncs.com/show/imgs/chat/avatar/1.jpg'
		}
	}
	return obj
}
/* 上面的可以删了 */

export default {
	namespaced:true,
	/* 属性值 */
	state: {
		//用户信息
		userInfo:{},
		//socket连接是否打开
		isSocketOpen:false,
		// 心跳计时器ID
		intervalId:null,
		// 计时器是否在运行
		onInterval:false,
		
		
		// 在线人员列表
		userInfoList:{},
		//在线人员信息
		userInfoObject:{},//UserInfoObject(),//上线前改为	userInfoObject:{}
		//聊天记录
		chatRecord:{},
		//聊天信息提醒记录
		chatReminder:[],
		
		//好友添加申请
		addRequestList:[],
		
	},
	
	/* 
		计算属性 
		定义 doneTodos:(state, getters,rootState)=>{}
		使用 store.getters.doneTodosCount
	*/
	getters: {
		userInfoList(state){
			const list =List(),user=state.userInfoObject
			for (const key in user){
				const name=user[key].nickname||''
				const label=PingYing.getFirstCamelChars(name).toUpperCase()
				const index=(Sort[label] === 0  || Sort[label]) ?Sort[label]:26
				list[index].data.push(user[key])
			}
			return list.filter(item=>{
				return item.data.length>0
			})
			
		}
	},
	/* 
		计算方法
		定义 increment :function(state,data){}
		使用 store.commit('increment', data) or  store.commit({type:'increment',...data})
	 */
	mutations: {
		//设置用户信息
		setUserInfo(state,data={}){
			state.userInfo={...state.userInfo,...data}
		},
		//退出时请空信息
		logout(state){
			state.userInfo={};
			state.onLineArray={};
			state.userInfoObject={};
			state.chatRecord={};
			state.chatReminder=[];
		},
		
		//设轩socket状态
		setSocket(state,data=false){
			state.isSocketOpen=data
		},
		//设置心跳计显示器ID
		setIntervalId(state,data=null){
			state.intervalId=data
		},
		//设置计时器状态
		setInterval(state,data){
			state.onInterval=data
		},
		// 时入对话页，取消末读记录 进入页面调用
		markRead(state,id){
			const index= state.chatReminder.findIndex(item=>{
				return item.receive === id
			})
			if(index>-1){
				Vue.set(state.chatReminder[index],'number',0)
			}
		},
		
		
		...User,
		...Private,
		
		
		
	},
	/* 
		导步计算方法
		定义 incrementAsync  :function ({ commit, state,dispatch,rootState  },data){commit('increment',data)}
		使用 store.dispatch('incrementAsync', data) or  store.commit({type:'incrementAsync',...data})
	 */
	actions: {
		
	}
}